<template>
  <div class="infoChange-container">
    <el-form ref="formRef" :model="form" class="infoChange-form" :rules="rules">
      <div class="title-container">
        <h3 class="title">用户信息管理</h3>
      </div>
      <el-form-item prop="name">
        <el-icon :size="20" class="svg-container">
          <User />
        </el-icon>
        <el-input v-model="form.name" placeholder="  name"></el-input>
      </el-form-item>
      <el-form-item prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio label="男"> 男</el-radio>
          <el-radio label="女"> 女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="phone">
        <el-icon :size="20" class="svg-container">
          <Phone />
        </el-icon>
        <el-input v-model="form.phone" placeholder="  phoneNumber"></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-icon :size="20" class="svg-container">
          <Message />
        </el-icon>
        <el-input v-model="form.email" placeholder="  email"></el-input>
      </el-form-item>
      <el-button type="primary" class="change-button" @click="handleChange">修改密码</el-button>
      <el-button type="primary" class="change-button save-button" @click="handleSave">保存</el-button>
    </el-form>
  </div>
  <div id="errorContainer">
    <div v-if="savedSuccessfully" class="success-message">保存成功！</div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { User, Phone, Message } from '@element-plus/icons-vue'
import { getInformation } from '@/api/getInformation'
import { change } from '@/api/infoChange'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

const form = ref({
  name: '',
  gender: '',
  phone: '',
  email: ''
})

const rules = ref({
  name: [
    {
      required: false,
      message: 'Please input Activity id',
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: false,
      message: 'Please select your gender',
      trigger: 'change'
    }
  ],
  phone: [
    {
      required: false,
      trigger: 'blur'
    }
  ],
  email: [
    {
      required: false,
      trigger: 'blur'
    }
  ]
})

const handleChange = () => {
  router.push('./pswChange')
}

const formRef = ref(null)

const savedSuccessfully = ref(false)

const getInfo = async () => {
  const res = await getInformation()
  console.log(res)
  form.value.name = res.data.data.name
  form.value.gender = res.data.data.gender
  form.value.phone = res.data.data.phone
  form.value.email = res.data.data.email
}

const handleSave = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      console.log('提交！')
      const changeResult = await change(form.value)

      if (!changeResult.success) {
        ElMessage.success('保存成功')
        savedSuccessfully.value = true
      } else {
        console.log('保存失败:', changeResult.error)
      }
    } else {
      console.log('提交出错！')
    }
  })
}

onMounted(getInfo)
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.infoChange-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .infoChange-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    :deep(.el-form-item) {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    :deep(.el-input) {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }

    .change-button {
      width: 48%;
      box-sizing: border-box;
    }

    .save-button {
      margin-top: 0%;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 10px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
    position: relative;
    top: -5px;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    :deep(.lang-select) {
      position: absolute;
      top: 4px;
      right: 0;
      background-color: white;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
